package com.config;

import com.Filter.LoginFilter;
import com.Filter.OrderFilter;
import com.pojo.Student;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//@Configuration      //相当于定义一个Spring的配置文件（applicationContext.xml）
public class MyConfig {
    @Bean
    public Student getStudent1(){
        System.out.println("执行了 getStudent1方法");
        Student student = new Student();
        student.setSname("xxx");
        student.setAge("20");
        return student;
    }

    @Bean
    public Student getStudent2(){
        System.out.println("执行了 getStudent2方法");
        Student student = new Student();
        student.setSname("getStudent2");
        student.setAge("20");
        return student;
    }


    @Bean
    public Student Student(){
        System.out.println("执行了 Student方法");
        Student student = new Student();
        student.setSname("Student");
        student.setAge("20");
        return student;
    }
    @Bean
    public FilterRegistrationBean myFilter1(){
        FilterRegistrationBean f = new FilterRegistrationBean();
        f.setOrder(1);//优先级，越小，越先执行。
        LoginFilter f1 = new LoginFilter();//创建对象
        f.setFilter(f1);//把Filter类注入到FilterRegistrationBean对象中
        Map map = new HashMap<>();
        map.put("age","20");
        f.setInitParameters(map);//初始化过滤器参数
        f.setName("LoginFilter");//相当于@WebFilter(filterName = )
        List list = new ArrayList();
        list.add(0,"/*");
        f.setUrlPatterns(list);//过滤器要过滤的路径
        return f;
    }

    @Bean
    public FilterRegistrationBean myFilter2(){
        FilterRegistrationBean f = new FilterRegistrationBean();
        f.setOrder(2);
        OrderFilter f1 = new OrderFilter();
        f.setFilter(f1);
        f.setName("OrderFilter");
        List list = new ArrayList();
        list.add(0,"/*");
        f.setUrlPatterns(list);
        return f;
    }
}
